package com.lili.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lili.controller.request.XasRoleRequest;
import com.lili.mapper.XasPermissionMapper;
import com.lili.mapper.XasRoleMapper;
import com.lili.mapper.XasRolePermMapper;
import com.lili.po.XasCouponList;
import com.lili.po.XasPermission;
import com.lili.po.XasRole;
import com.lili.po.XasRolePerm;
import com.lili.uitl.ShiroUtils;
import com.lili.vo.XasRoleVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PermService {
    @Autowired
    XasPermissionMapper xasPermissionMapper;
    @Autowired
    XasRoleMapper xasRoleMapper;
    @Autowired
    XasRolePermMapper xasRolePermMapper;
    @Autowired
    UserService userService;

    public List<XasPermission> xasPermissionList(Integer pid) {
        LambdaQueryWrapper<XasPermission> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(XasPermission::getPid, pid);
        List<XasPermission> xasPermissions = xasPermissionMapper.selectList(lambdaQueryWrapper);
        return xasPermissions;
    }


    public List<XasPermission> xasPermissionList() {
        List<XasPermission> xasPermissions = xasPermissionList(0);
        for (XasPermission xasPermission : xasPermissions) {
            List<XasPermission> xasPermissionsSun = xasPermissionList(xasPermission.getId());
            xasPermission.setXasPermission(xasPermissionsSun);
        }
        return xasPermissions;
    }


    public List<XasPermission> xasPermissionListByUid(Integer pid, Integer uid) {
        List<XasPermission> xasPermissions = xasPermissionMapper.xasPermissionListByUid(pid, uid);
        return xasPermissions;
    }

    public List<XasPermission> xasPermissionListByUid(Integer uid) {
        List<XasPermission> xasPermissions = xasPermissionListByUid(0, uid);
        for (XasPermission xasPermission : xasPermissions) {
            List<XasPermission> xasPermissionsSun = xasPermissionListByUid(xasPermission.getId(), uid);
            xasPermission.setXasPermission(xasPermissionsSun);
        }
        return xasPermissions;
    }


    @Transactional
    public void addXasRole(XasRoleRequest xasRoleRequest) {
        XasRole xasRole = new XasRole();
        BeanUtils.copyProperties(xasRoleRequest, xasRole);
        String userLoginName = ShiroUtils.getUserLoginName(userService);
        xasRole.setCreateUser(userLoginName);
        xasRole.setCreateTime(new Date());
        int insert = xasRoleMapper.insert(xasRole);
        for (Integer perm : xasRoleRequest.getPerms()) {
            XasRolePerm xasRolePerm = new XasRolePerm();
            xasRolePerm.setPid(perm);
            xasRolePerm.setRid(xasRole.getId());
            xasRolePermMapper.insert(xasRolePerm);
        }
    }

    @Transactional
    public void updateXasRole(XasRoleRequest xasRoleRequest) {
        XasRole xasRole = new XasRole();
        BeanUtils.copyProperties(xasRoleRequest, xasRole);
        xasRoleMapper.updateById(xasRole);
        LambdaQueryWrapper<XasRolePerm> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(XasRolePerm::getRid, xasRoleRequest.getId());
        xasRolePermMapper.delete(lambdaQueryWrapper);
        for (Integer perm : xasRoleRequest.getPerms()) {
            XasRolePerm xasRolePerm = new XasRolePerm();
            xasRolePerm.setPid(perm);
            xasRolePerm.setRid(xasRole.getId());
            xasRolePermMapper.insert(xasRolePerm);
        }
    }

    public void delXasRole(Integer id) {
        xasRoleMapper.deleteById(id);
    }


    public IPage<XasRole> selectXasRole(XasRoleRequest xasRoleRequest) {
        Page page = new Page();
        BeanUtils.copyProperties(xasRoleRequest, page);
        LambdaQueryWrapper<XasRole> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(xasRoleRequest.getName()), XasRole::getRoleName, xasRoleRequest.getName());
        IPage<XasRole> iPage = xasRoleMapper.selectPage(page, lambdaQueryWrapper);
        return iPage;
    }

    public List<XasRole> selectXasRole() {
        List<XasRole> xasRoles = xasRoleMapper.selectList(null);
        return xasRoles;
    }


    public XasRoleVo selectXasRoleById(Integer id) {
        XasRole xasRole = xasRoleMapper.selectById(id);
        XasRoleVo xasRoleVo = new XasRoleVo();
        BeanUtils.copyProperties(xasRole, xasRoleVo);
        LambdaQueryWrapper<XasRolePerm> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(XasRolePerm::getRid, id);
        List<XasRolePerm> xasRolePerms = xasRolePermMapper.selectList(lambdaQueryWrapper);
        List<Integer> collect = xasRolePerms.stream().map(e -> e.getPid()).collect(Collectors.toList());
        xasRoleVo.setPerms(collect);
        return xasRoleVo;
    }
}
